package com.erp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.comm.DataAndView;
import com.erp.comm.ResultCode;
import com.erp.comm.TreeNode;
import com.erp.pojo.DeptManager;
import com.erp.service.DeptService;
import com.erp.vo.DeptVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("dept")
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @RequestMapping("loadAllDept")

    public DataAndView loadAllDept(DeptVo deptVo) {
        IPage<DeptManager> page = new Page<DeptManager>(deptVo.getPage(), deptVo.getLimit());

        QueryWrapper<DeptManager> qw = new QueryWrapper<DeptManager>();

        if (deptVo.getTitle() != null) {
            qw.like("title", deptVo.getTitle());
        }

        if (deptVo.getAddress() != null) {
            qw.like("address", deptVo.getAddress());
        }
        if (deptVo.getRemark() != null) {
            qw.like("remark", deptVo.getRemark());
        }

        deptService.page(page, qw);


        deptService.page(page);
        return new DataAndView(page.getRecords());
    }

    @RequestMapping("loadDeptManagerLeftTreeJson")
    public DataAndView loadDeptManagerLeftTreeJson() {

        //查询所有部门信息
        List<DeptManager> lists = deptService.list();

        List<TreeNode> davs = new ArrayList<>();

        for(DeptManager dept:lists){
            boolean spread = dept.getOpen() == 0?false:true;
            TreeNode tn  = new TreeNode(dept.getId(),dept.getTitle(),spread,"0",dept.getPid());
            davs.add(tn);
        }

        return new DataAndView(davs);

    }




    //添加
    @RequestMapping("loadDeptMaxOrderNum")
    public Map<String, Object> loadDeptMaxOrderNum() {

        Integer maxOrderNum = null;
        QueryWrapper<DeptManager> qw = new QueryWrapper();
        qw.orderByDesc("ordernum");

        List<DeptManager> lists = deptService.list(qw);
        maxOrderNum = lists.get(0).getOrdernum();

        Map map = new HashMap<String, Object>();

        if (lists.size() > 0) {
            map.put("value", maxOrderNum + 1);
        } else {
            map.put("value", 1);
        }
        return map;
    }



//    @RequestMapping("addDept")
//    public ResultCode addDept(){
//
//        return null;
//
//    }

    @RequestMapping("addDept")
    public ResultCode addDept(DeptVo deptVo) {
        deptVo.setId(null);
        try {
            boolean bool = deptService.save(deptVo);
            if(bool){
                return ResultCode.SUCCESS_ADD;
            }else{
                return ResultCode.FAIL_ADD;
            }
        }catch (Exception e) {
            return ResultCode.FAIL_ADD;
        }
    }


    //更新

    //删除

}
